package UserTable;

import user.User;
import utils.JdbcUtils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class UpdataUser {

    public static void init(int power,String name){
        JFrame frame=  new JFrame("修改");
        //设置窗体大小
        frame.setSize(600, 400);
        //设置窗体居中
        frame.setLocationRelativeTo(null);
        //取消布局
        frame.setLayout(null);
        //设置窗口不能改变大小
        frame.setResizable(false);
        //设置窗体是否可见
        frame.setVisible(true);
        List<Map<String, Object>> re= JdbcUtils.JdbcQuery(User.sql_query,name);
        String password=null;
        for(Map<String,Object> map : re){
            for(String key : map.keySet()) {
                if(key.equals("user_password"))password= (String) map.get(key);
            }
        }
        //原密码
        Box passbox=Box.createHorizontalBox();
        JLabel pass_a = new JLabel("  原密码为:");
        JPasswordField pass_b=new JPasswordField(15);
        pass_b.setEchoChar('*');
        passbox.add(pass_a);
        passbox.add(Box.createHorizontalStrut(10));
        passbox.add(pass_b);
        passbox.setBounds(30,50,400,30);
        frame.add(passbox);
        //修改密码
        Box passnew_box=Box.createHorizontalBox();
        JLabel passnew_a = new JLabel("修改密码为:");
        JPasswordField passnew_b=new JPasswordField(15);
        passnew_b.setEchoChar('*');
        passnew_box.add(passnew_a);
        passnew_box.add(Box.createHorizontalStrut(10));
        passnew_box.add(passnew_b);
        passnew_box.setBounds(30,100,400,30);
        frame.add(passnew_box);
        //检验密码
        Box passyn_box=Box.createHorizontalBox();
        JLabel passyn_a = new JLabel("重新输入:");
        JPasswordField passyn_b=new JPasswordField(15);
        passyn_b.setEchoChar('*');
        passyn_box.add(passyn_a);
        passyn_box.add(Box.createHorizontalStrut(10));
        passyn_box.add(passyn_b);
        passyn_box.setBounds(30,150,400,30);
        frame.add(passyn_box);
        //按钮盒子
        Box btnbox=Box.createHorizontalBox();
        //提交修改按钮
        JButton subBtn=new JButton("修改");
        //取消修改按钮
        JButton noBtn=new JButton("返回");
        btnbox.add(subBtn);
        btnbox.add(Box.createHorizontalStrut(60));
        btnbox.add(noBtn);
        btnbox.setBounds(150,300,400,50);
        frame.add(btnbox);

        noBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int yn=JOptionPane.showConfirmDialog(frame,"确定退出吗？","提示", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);	//确认对话框
                if(yn==JOptionPane.OK_OPTION) frame.dispose();
            }
        });
        String finalPassword = password;
        subBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String be=pass_b.getText().trim();
                String now=passnew_b.getText().trim();
                String nowag=passyn_b.getText().trim();
                if(!now.equals(nowag)|| !Objects.equals(finalPassword, be)){
                    JOptionPane.showMessageDialog(frame,((!Objects.equals(finalPassword, be))?"原密码错误\n": "")+((!now.equals(nowag)) ? "两次密码不同" : ""));
                }else{
                    int yn=JOptionPane.showConfirmDialog(frame,"确定提交修改吗？","提示", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
                    if(yn==JOptionPane.OK_OPTION) {
                        JdbcUtils.JdbcUpdate(User.sql_updata_password,now,name);
                        JOptionPane.showMessageDialog(frame,"修改成功");
                        frame.dispose();
                    }
                }
            }
        });
    }
}
